/**文件名：LoginService.java*版权：MoonBox*修改人：Peter*修改时间：2018 年10月25日*修改内容：新增*/
package com.renzhebiao.lkang.service.impl;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.renzhebiao.lkang.base.BaseService;
import com.renzhebiao.lkang.dto.LoginDTO;
import com.renzhebiao.lkang.entity.UserPO;
import com.renzhebiao.lkang.exception.ForbiddenException;
import com.renzhebiao.lkang.mapper.UserMapper;
import com.renzhebiao.lkang.service.ILoginService;
import com.renzhebiao.lkang.utils.PasswordEncoder;

/***
 * TODO 添加类的一句话简单描述。*
 * <p>
 * *TODO 详细描述**
 * 
 * @author Peter*@since 1.0.0
 */
@Service
public class LoginService extends BaseService<UserPO> implements ILoginService {

    @Autowired
    UserMapper      userMapper;
    // private BCryptPasswordEncoder encoder = new BCryptPasswordEncoder(12);
    @Autowired
    PasswordEncoder passwordEncoder;

    @Override
    public UserPO loginAdmin(LoginDTO login) {
        return loginBaic(login);
    }

    /**
     * 公共登陆校验
     *
     * @param userName 用户名
     * @return 用户基本信息
     */
    private UserPO loginBaic(LoginDTO login) {
        UserPO user = this.userMapper.selectOne(UserPO.builder().userName(login.getUserName()).build());

        if (null == user) {
            throw new ForbiddenException(40302, "用户名或密码错误");
        } else if (!passwordEncoder.isPasswordValid(user.getPassword(), login.getPassword())) {
            throw new ForbiddenException(40302, "用户名或密码错误");
        }
        return user;
    }

}
